package cz.semjob.learning.relation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import cz.semjob.document.annotation.RelationWordsPair;
import cz.semjob.document.annotation.metadata.AnnotationTypeFactory;
import cz.semjob.document.processing.DocumentInstance;
import cz.semjob.learning.data.TaxonomyLearningException;
import cz.semjob.learning.data.attribute.NumericInstanceAttribute;
import cz.semjob.learning.instances.UnlabeledRelationInstance;
import cz.semjob.learning.instances.creator.BaseTextInstanceCreator;

@Component
public class TaxonomicRelationInstanceCreator extends
		BaseRelationInstanceCreator {

	@Autowired
	private AnnotationTypeFactory annotationTypeFactory;

	/**
	 * 
	 */
	private static final long serialVersionUID = -2524727349900287891L;

	public static final String WORDS_DISTANCE = "WordsDistance";

	private NumericInstanceAttribute wordsDistanceAttribute;

	@Autowired
	public TaxonomicRelationInstanceCreator(
			@Qualifier("basetextinstancecreator") BaseTextInstanceCreator textInstanceCreator,
			AnnotationTypeFactory annotationTypeFactory) {
		super(textInstanceCreator, annotationTypeFactory);

		attributesNamesList.add(WORDS_DISTANCE);
	}

	public void initTextInstanceCreator() {
		super.initTextInstanceCreator();

		wordsDistanceAttribute = new NumericInstanceAttribute(WORDS_DISTANCE);
		attributesMap.put(WORDS_DISTANCE, wordsDistanceAttribute);
		updateAttributes(attributesNamesList);
	}

	protected void fillRelationInstanceAttributes(
			DocumentInstance documentInstance,
			RelationWordsPair relationWordsPair,
			UnlabeledRelationInstance unlabeledInstance)
			throws TaxonomyLearningException {

		super.fillRelationInstanceAttributes(documentInstance,
				relationWordsPair, unlabeledInstance);
		if (attributesNamesList.contains(WORDS_DISTANCE)) {
			int wordsDistance = this.textInstanceCreator
					.getAnnotationsTextDistance(
							relationWordsPair.getRelationWord1(),
							relationWordsPair.getRelationWord2());
			unlabeledInstance.addAttributeValue(WORDS_DISTANCE, wordsDistance);
		}
	}

	public String getName() {
		return "TaxonomicRelationInstanceCreator";
	}

	public void setAnnotationTypeFactory(
			AnnotationTypeFactory annotationTypeFactory) {
		this.annotationTypeFactory = annotationTypeFactory;
	}
}
